import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class PrivacyPolicyDialog extends StatefulWidget {
  final VoidCallback? onAgree;
  final VoidCallback? onDisagree;

  const PrivacyPolicyDialog({
    super.key,
    this.onAgree,
    this.onDisagree,
  });

  @override
  State<PrivacyPolicyDialog> createState() => _PrivacyPolicyDialogState();
}

class _PrivacyPolicyDialogState extends State<PrivacyPolicyDialog> {
  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(8),
        side: const BorderSide(color: Colors.grey, width: 1),
      ),
      titlePadding: const EdgeInsets.fromLTRB(20, 24, 20, 0),
      contentPadding: const EdgeInsets.fromLTRB(20, 16, 20, 0),
      actionsPadding: const EdgeInsets.fromLTRB(20, 24, 20, 20),
      title:  Text(
        '用户协议及隐私保护',
        style: TextStyle(
          fontSize: 16.sp,
          fontWeight: FontWeight.bold,
        ),
        textAlign: TextAlign.center,
      ),
      content: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          const SizedBox(height: 12),
          RichText(
            textAlign: TextAlign.center,
            text: TextSpan(
              children: [
                TextSpan(
                  text:'为了更好的保障您的合法权益，请您阅读并同意以下协议',
                  style: TextStyle(fontSize: 14, color: Colors.black87),
                ),
                _buildLinkSpan('《用户协议》', onTap: () => _showPolicy(0)),
                const TextSpan(text: '  '),
                _buildLinkSpan('《隐私政策》', onTap: () => _showPolicy(1)),
              ],
            ),
          ),
        ],
      ),
      actions: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              child: TextButton(
                onPressed: () {
                  widget.onDisagree?.call();
                  Navigator.of(context).pop();
                },
                style: TextButton.styleFrom(
                  padding: const EdgeInsets.symmetric(vertical: 12),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(6),
                  ),
                ),
                child: const Text(
                  '不同意',
                  style: TextStyle(color: Colors.black, fontSize: 16),
                ),
              ),
            ),
            Expanded(
              child: ElevatedButton(
                onPressed: () {
                  widget.onAgree?.call();
                  Navigator.of(context).pop();
                },
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.blue[700],
                  padding: const EdgeInsets.symmetric(vertical: 12),
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(6),
                  ),
                ),
                child: const Text(
                  '同意',
                  style: TextStyle(color: Colors.white, fontSize: 16),
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }

  TextSpan _buildLinkSpan(String text, {required VoidCallback onTap}) {
    return TextSpan(
      text: text,
      style: const TextStyle(
        color: Colors.blue,
        decoration: TextDecoration.underline,
      ),
      recognizer: TapGestureRecognizer()..onTap = onTap,
    );
  }

  void _showPolicy(int index) {
    final policies = [
      '《用户协议》内容：\n\n这里是用户协议的详细内容...',
      '《隐私政策》内容：\n\n这里是隐私政策的详细内容...',
    ];

    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text(index == 0 ? '用户协议' : '隐私政策'),
        content: Text(policies[index]),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(),
            child: const Text('关闭'),
          )
        ],
      ),
    );
  }
}